package org.example.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisUtils {

    @Autowired
    private RedisTemplate<Serializable, Object> redisTemplate;

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 写入缓存
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean set(final String key, Object value,long var3, TimeUnit var5) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value,var3,var5);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新缓存
     */
    public boolean getAndSet(final String key, Object value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除缓存
     */
    public boolean delete(final String key) {
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public long increment(final String key){
        Long increment = redisTemplate.opsForValue().increment(key, 1);
        redisTemplate.expire(key,2,TimeUnit.DAYS);
        return increment;
    }

    public void expire(String key,long timeout,TimeUnit timeUnit){
        try{
            redisTemplate.expire(key,timeout,timeUnit);
        }catch (Exception e){

        }
    }

    public boolean hset(String key,String entryKey, Object value,long timeout,TimeUnit timeUnit){
        try{
            redisTemplate.opsForHash().put(key,entryKey,value);
            if(timeout>0){
                expire(key,timeout,timeUnit);
            }
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return false;
    }

    public Object hget(String key,String entryKey){
        try{
            return redisTemplate.opsForHash().get(key,entryKey);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }
    public void hdel(String key, String entryKey) {
        try {
            redisTemplate.opsForHash().delete(key, entryKey);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
